﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pathfinder.Base.Constructs;
using System.Collections.ObjectModel;
using Bridge.Base.Constructs;
using Bridge.UI.Utilities;

namespace Pathfinder.Base.UI
{
    public class ArmorClassView : ChangableObject
    {
        public ArmorClassView(ACType acType, ObservableCollection<ACType> possibleValues, int value, PathfinderCharacterView pathfinderCharacterView)
        {
            mACType = acType;
            PossibleACs = possibleValues;
            PathfinderCharacterView = pathfinderCharacterView;
            mValue = value;
        }

        public ObservableCollection<ACType> PossibleACs
        {
            get { return mPossibleACs; }
            set { CheckPropertyChanged<ObservableCollection<ACType>>("PossibleACs", ref mPossibleACs, ref value); }
        }

        public PathfinderCharacterView PathfinderCharacterView
        {
            get { return mPathfinderCharacterView; }
            private set { CheckPropertyChanged<PathfinderCharacterView>("PathfinderCharacterView", ref mPathfinderCharacterView, ref value); }
        }

        public ACType ACType
        {
            get { return mACType; }
            set
            {
                ACType oldACType = mACType;
                if (CheckPropertyChanged<ACType>("ACType", ref mACType, ref value))
                {
                    PathfinderCharacterView.ChangeAC(oldACType, value, Value);
                    NotifyPropertyChanged("AffectsTH");
                    NotifyPropertyChanged("AffectsFF");
                }
            }
        }

        public int Value
        {
            get { return mValue; }
            set
            {
                if (CheckPropertyChanged<int>("Value", ref mValue, ref value))
                {
                    PathfinderCharacterView.Character.SetAC(ACType, value);
                    NotifyPropertyChanged("StringValue");
                }
            }

        }

        public string StringValue
        {
            get { return mValue.ToString(); }
            set
            {
                int possibleValue = -1;
                if (int.TryParse(value, out possibleValue))
                {
                    Value = possibleValue;
                    IsValid = true;
                }
                else
                {
                    IsValid = false;
                }
            }
        }

        public string AffectsFF
        {
            get
            {
                ACTypeEffectAttribute attribute = ACType.GetCustomAttribute<ACTypeEffectAttribute>();
                if (attribute != null && attribute.EffectsFlatFooted)
                {
                    return "FF";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string AffectsTH
        {
            get
            {
                ACTypeEffectAttribute attribute = ACType.GetCustomAttribute<ACTypeEffectAttribute>();
                if (attribute != null && attribute.EffectsTouch)
                {
                    return "TH";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public bool IsValid
        {
            get { return mIsValid; }
            set { CheckPropertyChanged<bool>("IsValid", ref mIsValid, ref value); }
        }

        private ObservableCollection<ACType> mPossibleACs = new ObservableCollection<ACType>();
        private ACType mACType = ACType.Other;
        private PathfinderCharacterView mPathfinderCharacterView;
        private int mValue = 0;
        private bool mIsValid = true;
    }
}
